package com.example.myapplication14_5;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
    private List<VideoItem> videoList;
    private OnVideoClickListener onVideoClickListener;

    public interface OnVideoClickListener {
        void onVideoClick(VideoItem video, int position);
    }

    public VideoAdapter(OnVideoClickListener listener) {
        this.videoList = new ArrayList<>();
        this.onVideoClickListener = listener;
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false);
        return new VideoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        VideoItem video = videoList.get(position);
        holder.videoTitle.setText(video.getTitle());
        holder.videoPath.setText(video.getPath());
        holder.videoSize.setText(video.getFormattedSize());
        holder.videoDuration.setText(video.getDuration());

        holder.itemView.setOnClickListener(v -> {
            if (onVideoClickListener != null) {
                onVideoClickListener.onVideoClick(video, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }

    public void updateVideoList(List<VideoItem> newList) {
        this.videoList.clear();
        this.videoList.addAll(newList);
        notifyDataSetChanged();
    }

    static class VideoViewHolder extends RecyclerView.ViewHolder {
        TextView videoTitle;
        TextView videoPath;
        TextView videoSize;
        TextView videoDuration;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            videoTitle = itemView.findViewById(R.id.videoTitle);
            videoPath = itemView.findViewById(R.id.videoPath);
            videoSize = itemView.findViewById(R.id.videoSize);
            videoDuration = itemView.findViewById(R.id.videoDuration);
        }
    }
}